package test;

import jm.JMC;
import jm.music.data.Note;
import jm.music.data.Part;
import jm.music.data.Phrase;
import jm.music.data.Score;
import jm.util.Play;
import jm.util.View;
import jm.util.Write;

/**
 * A short example which generates a chromatic melody
 * which follows the curve of a sine wave
 * and writes to a MIDI file
 * @author Andrew Brown and Rene Wooller
 */
public final class SineMelody implements JMC{
    public static void main(String[] args){
        Score s = new Score("JMDemo - Sine melody");
        Part p = new Part("Melody", MARIMBA, 0);
        Phrase phr = new Phrase(); 
        // the density is the resolution of notes over the sine wave function
        // a lower density produces a shorter melody - more course resolution.
        double density = 25.0;
        
        // create a phrase of randomly pitched quavers over the full MIDI range.
        for(int i = 0; i < (2*3.14*density); i++) { /*3.14 is an approximate 
                                                      value for pi */
                int pitch = (int)(Math.sin(i/density) * 50 + 60);
                Note note = new Note(pitch, DSQ);
                phr.addNote(note);
        }
        
        // add the phrase to an instrument and that to a score
        p.addPhrase(phr);
        s.addPart(p);
        
        //display the output
        View.show(s);
        
        // create a MIDI file of the score
        Play.midi(s);
//        Write.midi(s, "SineMelody.mid");
    }
}
